######### 调用示例 ###########
# sh run-docker.sh hzero-register
# sh run-docker.sh hzero-register 8000
# sh run-docker.sh hzero-register/
#
# 自动docker部署微服务
##############################

# 使用国内 daocloud 一键安装Docker软件的命令
# curl -sSL https://get.daocloud.io/docker | sh

FRONT_PORT=80       #HZERO前端应用(hzero-front)暴露的IP端口，默认80，可自行修改
REGISTER_PORT=8000  #注册服务(hzero-register)暴露的IP端口，默认8000，可自行修改
CONFIG_PORT=8010    #配置服务(hzero-config)暴露的IP端口，默认8010，可自行修改
GATEWAY_PORT=8080   #网关服务(hzero-gateway)暴露的IP端口，默认8080，可自行修改
##### 如下服务的端口建议不做修改(端口冲突除外)，一般不会被外部调用 #####
OAUTH_PORT=8020     #认证服务(hzero-oauth)暴露的IP端口，默认8020，可自行修改
IAM_PORT=8030       #IAM服务(hzero-iam)暴露的IP端口，默认8030，可自行修改
PLATFORM_PORT=8100  #平台服务(hzero-platform)暴露的IP端口，默认8100，可自行修改
SWAGGER_PORT=8050   #Swagger测试服务(hzero-swagger)暴露的IP端口，默认8050，可自行修改
ADMIN_PORT=8060     #平台治理服务(hzero-admin)暴露的IP端口，默认8060，可自行修改
FILE_PORT=8110      #文件服务(hzero-file)暴露的IP端口，默认8110，可自行修改
MESSAGE_PORT=8120   #消息服务(hzero-message)暴露的IP端口，默认8120，可自行修改
SCHEDULER_PORT=8130 #调度服务(hzero-scheduler)暴露的IP端口，默认8130，可自行修改
IMPORT_PORT=8140    #导入服务(hzero-import)暴露的IP端口，默认8140，可自行修改
REPORT_PORT=8210    #报表服务(hzero-report)暴露的IP端口，默认8210，可自行修改

if [ -z "$1" ]; then
  read -p ">>>请输入运行的HZERO服务名称：" SERVICE_NAME
  SERVICE_NAME=${SERVICE_NAME,,} #参数转换成小写字符
else
  SERVICE_NAME=${1,,} #参数转换成小写字符
fi
SERVICE_NAME=${SERVICE_NAME%"/"} #删除末尾的"/"符

if [ -n "$2" ];then
  if  [[ ! $2 =~ ^-?[0-9]+$ ]] || [ $2 -le 0 ] || [ $2 -gt 65535 ];then
    echo ">>>输入值不正确，端口号必须为不大于65535的正整数！"
    echo "   example: sh run-docker.sh hzero-register 8000"
    exit 1
  fi
  SPORT=$2
fi

IPADDRESS=$(/sbin/ifconfig eth0|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:') #获取本机非docker网卡及非回环网卡的IP地址，也可自行指定值
if [ -z "$IPADDRESS" ]; then
  echo ">>>开始扫描服务器网卡及对应IP地址..."
  networknames=`ifconfig |grep -e "\beth[0-9]*:" -e "\bens[0-9]*:"|awk '{print $1}'`
  arr=($networknames)
  for net in ${arr[@]}
  do
    ip=`ifconfig ${net%":"} |grep -v inet6|grep inet| awk '{print $2}'`
    echo ">>> $net $ip"
  done
  
  if [ ${#arr[*]} == 1 ] && [ -n $ip ];then #仅检测到一个网卡，直接获取其IP地址
    read -p "是否使用 $ip 作为$1服务的注册IP地址? [Y/n]  " yn
    if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then 
      IPADDRESS=$ip
    fi
  fi
  
  if [ -z "$IPADDRESS" ]; then
    read -p ">>>请指定$1服务的注册IP地址：[$ip]    " IPADDRESS
    if [ -z "$IPADDRESS" ] && [ -n "$ip" ];then
      IPADDRESS=$ip
    fi
  fi
else
  echo ">>>Current Ip Address: $IPADDRESS"
  ip=$IPADDRESS
  read -p "是否使用 $ip 作为$1服务的注册IP地址? [Y/n]    " yn
  if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
    IPADDRESS=$ip
  else
    read -p ">>>请指定$1服务的注册IP地址：[$ip] " IPADDRESS
    if [ -z "$IPADDRESS" ] && [ -n "$ip" ];then
          IPADDRESS=$ip
    fi
  fi
fi

FRONT_INSTALL_HOST=$IPADDRESS   #HZERO前端应用安装地址，默认为本机地址，可自行修改
HZERO_INSTALL_HOST=$IPADDRESS   #HZERO后端服务安装地址，默认为本机地址，可自行修改
DB_INSTALL_HOST=$IPADDRESS      #MySQL数据库安装地址，默认为本机地址，可自行修改
DB_INSTALL_PORT=3306            #MySQL数据库安装端口，默认为3306，可自行修改
REDIS_INSTALL_HOST=$IPADDRESS   #Redis缓存安装地址，默认为本机地址，可自行修改
REDIS_INSTALL_PORT=6379         #Redis缓存安装端口，默认为6379，可自行修改
CONFIG_ENABLED="false"          #是否启用config配置服务，默认为false，可自行修改

INIT_MEMORY=512m    #JVM初始运行内存，默认512M，可自行修改
MAX_MEMORY=1024m    #JVM最大运行内存，默认1024M，可自行修改

# 检测端口是否被占用
function check_ports(){
  local rs=0
  local port=""
  for port in $@
  do
    local grep_port=(`netstat -tlpn | grep ":\b$port\b" |awk '{print $7}'`)
    if [ -n "$grep_port" ];then
      local grep_server=(`docker ps --format '{{.Names}} {{.Ports}}' | grep ":\b$port\b" |awk '{print $1}'`)
      local grep_server2=(`docker ps --format '{{.Names}} {{.Ports}}' | grep ":\b$(($port-1))\b" |awk '{print $1}'`)
      #检测是否被自己的服务占用
      if ( [ -z $grep_server ] && [ -z $grep_server2 ] ) || ( [ "$SERVICE_NAME" != "$grep_server" ] && [ "$SERVICE_NAME" != "$grep_server2" ] );then
        echo ">>>  端口 $port 已被进程占用(PID/Program name)： $grep_port"
        rs=1
      fi
    #else
    #  echo "port $port is not in use"
    fi
  done
  if [ $rs == 1 ];then
    exit 1
  fi
}

if [ $SERVICE_NAME == "hzero-register" ];then 
  # 安装 hzero-register 服务
  if [ -z "$SPORT" ];then
    SPORT=$REGISTER_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8000" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-register
  # docker run -d --net=host --name hzero-register \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-register \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "EUREKA_CLIENT_REGISTER_WITH_EUREKA=true" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-register

elif [ $SERVICE_NAME == "hzero-config" ];then 
  # 安装 hzero-config 服务
  if [ -z "$SPORT" ];then
    SPORT=$CONFIG_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8010" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-config
  # docker run -d --net=host --name hzero-config \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-config \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_admin?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka/" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-config

elif [ $SERVICE_NAME == "hzero-gateway" ];then 
  # 安装 hzero-gateway 服务
  if [ -z "$SPORT" ];then
    SPORT=$GATEWAY_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8080" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-gateway
  # docker run -d --net=host --name hzero-gateway \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-gateway \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_platform?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-gateway

elif [ $SERVICE_NAME == "hzero-oauth" ];then 
  # 安装 hzero-oauth 服务
  if [ -z "$SPORT" ];then
    SPORT=$OAUTH_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8020" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-oauth
  # docker run -d --net=host --name hzero-oauth \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-oauth \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_platform?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    -e "HZERO_OAUTH_LOGIN_SUCCESS_URL=http://$FRONT_INSTALL_HOST:$FRONT_PORT/workplace" \
    -e "HZERO_OAUTH_SSO_SERVICE_BASE_URL=http://$HZERO_INSTALL_HOST:$GATEWAY_PORT/oauth" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-oauth

elif [ $SERVICE_NAME == "hzero-iam" ];then 
  # 安装 hzero-iam 服务
  if [ -z "$SPORT" ];then
    SPORT=$IAM_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8030" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-iam
  # docker run -d --net=host --name hzero-iam \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-iam \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_platform?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-iam

elif [ $SERVICE_NAME == "hzero-platform" ];then 
  # 安装 hzero-platform 服务
  if [ -z "$SPORT" ];then
    SPORT=$PLATFORM_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8100" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-platform
  # docker run -d --net=host --name hzero-platform \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-platform \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_platform?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-platform

elif [ $SERVICE_NAME == "hzero-swagger" ];then
  # 安装 hzero-swagger 服务
  if [ -z "$SPORT" ];then
    SPORT=$SWAGGER_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8050" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-swagger
  # docker run -d --net=host --name hzero-swagger \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-swagger \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_admin?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    -e "HZERO_SWAGGER_BASE_URL=http://$HZERO_INSTALL_HOST:$GATEWAY_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-swagger

elif [ $SERVICE_NAME == "hzero-admin" ];then 
  # 安装 hzero-admin 服务
  if [ -z "$SPORT" ];then
    SPORT=$ADMIN_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8060" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  docker rm -f hzero-admin
  set -x
  # docker run -d --net=host --name hzero-admin \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-admin \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_admin?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-admin

elif [ $SERVICE_NAME == "hzero-file" ];then
  # 安装 hzero-file 服务 
  if [ -z "$SPORT" ];then
    SPORT=$FILE_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8110" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-file
  # docker run -d --net=host --name hzero-file \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-file \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_file?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-file

elif [ $SERVICE_NAME == "hzero-message" ];then 
  # 安装 hzero-message 服务
  if [ -z "$SPORT" ];then
    SPORT=$MESSAGE_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8120" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-message
  # docker run -d --net=host --name hzero-message \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-message \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_message?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-message

elif [ $SERVICE_NAME == "hzero-scheduler" ];then 
  # 安装 hzero-scheduler 服务
  if [ -z "$SPORT" ];then
    SPORT=$SCHEDULER_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8130" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-scheduler
  # docker run -d --net=host --name hzero-scheduler \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-scheduler \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_scheduler?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-scheduler

elif [ $SERVICE_NAME == "hzero-import" ];then 
  # 安装 hzero-import 服务
  if [ -z "$SPORT" ];then
    SPORT=$IMPORT_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8140" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-import
  # docker run -d --net=host --name hzero-import \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-import \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_import?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-import

elif [ $SERVICE_NAME == "hzero-report" ];then 
  # 安装 hzero-report 服务
  if [ -z "$SPORT" ];then
    SPORT=$REPORT_PORT
  fi
  MPORT=$(($SPORT+1))
  check_ports $SPORT $MPORT
  if [ $SPORT != "8210" ];then 
    INSTANCE_ID="-Dserver.port=$SPORT -Dmanagement.server.port=$MPORT -Deureka.instance.instance-id=$IPADDRESS:\${spring.application.name}:$SPORT"
  fi
  set -x
  docker rm -f hzero-report
  # docker run -d --net=host --name hzero-report \
  docker run -d -p $SPORT:$SPORT -p $MPORT:$MPORT --name hzero-report \
    -e "JAVA_OPTS=-Xms$INIT_MEMORY -Xmx$MAX_MEMORY -Deureka.instance.ip-address=$IPADDRESS $INSTANCE_ID" \
    -e "SPRING_DATASOURCE_URL=jdbc:mysql://$DB_INSTALL_HOST:$DB_INSTALL_PORT/hzero_report?useUnicode=true&characterEncoding=utf-8&useSSL=false" \
    -e "SPRING_DATASOURCE_USERNAME=hzero" \
    -e "SPRING_DATASOURCE_PASSWORD=hzero" \
    -e "EUREKA_DEFAULT_ZONE=http://$HZERO_INSTALL_HOST:$REGISTER_PORT/eureka" \
    -e "SPRING_REDIS_HOST=$REDIS_INSTALL_HOST" \
    -e "SPRING_REDIS_PORT=$REDIS_INSTALL_PORT" \
    -e "SPRING_CLOUD_CONFIG_ENABLED=$CONFIG_ENABLED" \
    -e "SPRING_CLOUD_CONFIG_URI=http://$HZERO_INSTALL_HOST:$CONFIG_PORT" \
    --privileged=true --restart=always \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-report
    
elif [ $SERVICE_NAME == "hzero-front" ];then 
  # 安装 hzero-front 服务
  if [ -z "$SPORT" ];then
    SPORT=$FRONT_PORT
  fi
  check_ports $SPORT
  set -x
  docker rm -f hzero-front
  # docker run -d --net=host --name hzero-front \
  docker run -d -p $SPORT:80 --name hzero-front \
    -e "BUILD_BASE_PATH=/" \
    -e "BUILD_APP_NAME=hzero-front" \
    -e "BUILD_API_HOST=http://$FRONT_INSTALL_HOST:$GATEWAY_PORT" \
    -e "BUILD_CLIENT_ID=localhost" \
    -e "BUILD_WEBSOCKET_HOST=http://$FRONT_INSTALL_HOST:$GATEWAY_PORT/hpfm/sock-js" \
    -e "BUILD_CUSTOMIZE_ICON_NAME=customize-icon" \
    -e "BUILD_PLATFORM_VERSION=1.4.0" \
    -e "BUILD_TRACE_LOG_ENABLE=false" \
    -e "BUILD_MULTIPLE_SKIN_ENABLE=true" \
    -e "BUILD_TOP_MENU_LABELS=HZERO_MENU" \
    -e "BUILD_TOP_MENU_UNION_LABEL=false" \
    -e "BUILD_MULTIPLE_LANGUAGE_ENABLE=true" \
    -e "BUILD_ENV_SIGN=Docker部署环境" \
    -e "BUILD_SKIP_TS_CHECK_IN_START=true" \
    -e "BUILD_SKIP_ESLINT_CHECK_IN_START=true" \
    -e "SKIP_NO_CHANGE_MODULE=true" \
    -e "BUILD_NO_PROXY=true" \
    -e "BUILD_FORCE_PROD_DLL=true" \
    -e "BUILD_LOGIN_URL=http://$FRONT_INSTALL_HOST:$SPORT/workplace" \
    -e "BUILD_LOGOUT_URL=http://$FRONT_INSTALL_HOST:$GATEWAY_PORT/oauth/login" \
    registry.cn-hangzhou.aliyuncs.com/openhand/hzero-front
    
else
  echo "不存在该HZERO服务：$SERVICE_NAME"
fi
